from langchain.tools import StructuredTool

def safe_write_file(file_name: str, content: str) -> str:
    """仅允许写入 ./output/ 目录下的文件"""
    import os
    output_dir = "./output"
    os.makedirs(output_dir, exist_ok=True)
    file_path = os.path.join(output_dir, file_name)
    with open(file_path, "w") as f:
        f.write(content)
    return f"文件已保存至 {file_path}"

file_tool = StructuredTool.from_function(
    func=safe_write_file,
    name="FileWriter",
    description="将内容写入文件，路径限制在 ./output/"
)

